<%@page import="fi.foyt.cs.persistence.domainmodel.user.UserLocation"%>
<%@page import="fi.foyt.cs.controller.ItemController"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.items.StashedItem"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.items.InventoryItem"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.items.Inventory"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.oauth.AuthenticatedUserToken"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.oauth.AuthenticatedUserCode"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.user.UserIdentifier"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.oauth.AuthenticatedUser"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.user.User"%>
<%@page import="org.apache.commons.lang3.math.NumberUtils"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="fi.foyt.cs.controller.UserController"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<%@page import="fi.foyt.cs.persistence.domainmodel.oauth.AuthorizedClient"%>
<%@page import="fi.foyt.cs.controller.OAuthController"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.system.Property"%>
<%@page import="fi.foyt.cs.controller.SystemController"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.task.GeneralTask"%>
<%@page import="fi.foyt.cs.controller.TaskController"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.task.TaskType"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <jsp:include page="../fragments/head.jsp"></jsp:include>
</head>
<body>
	<jsp:include page="../fragments/header.jsp"></jsp:include>

	<%
	  UserController userController = new UserController();
    
	  Long userId = NumberUtils.createLong(request.getParameter("userId"));
	  User user = userController.findUserById(userId);
	  
	  pageContext.setAttribute("user", user);
	  
	  if ("POST".equals(request.getMethod())) {
	    OAuthController oAuthController = new OAuthController();
	    ItemController itemController = new ItemController();
	     
	    List<UserIdentifier> userIdentifiers = userController.listUserIdentifiersByUser(user);
	    for (UserIdentifier userIdentifier : userIdentifiers) {
	      AuthenticatedUser authenticatedUser = oAuthController.findAuthenticatedUserByUserId(userIdentifier.getIdentifier());
	      if (authenticatedUser != null) {
	        AuthenticatedUserCode authenticatedUserCode = oAuthController.findAuthenticatedUserCodeByAuthenticatedUser(authenticatedUser);
	        if (authenticatedUserCode != null) {
            out.append("<p> Deleting AuthenticatedUserCode #" + authenticatedUserCode.getId().getId() + "</p>");
	          oAuthController.deleteAuthenticatedUserCode(authenticatedUserCode);
	        }
	        
          List<AuthenticatedUserToken> authenticatedUserTokens = oAuthController.listAuthenticatedUserTokensByAuthenticatedUser(authenticatedUser);
	        for (AuthenticatedUserToken authenticatedUserToken : authenticatedUserTokens) {
	          out.append("<p> Deleting AuthenticatedUserToken #" + authenticatedUserToken.getId().getId() + "</p>");
	          oAuthController.deleteAuthenticatedUserToken(authenticatedUserToken);
	        }
          out.append("<p> Deleting AuthenticatedUser #" + authenticatedUser.getId().getId() + "</p>");
	        oAuthController.deleteAuthenticatedUser(authenticatedUser);
	      }
	      
	      Inventory inventory = userController.findInventoryByUser(user);
	      if (inventory != null) {
	        List<InventoryItem> inventoryItems = userController.listInventoryItems(inventory);
	        for (InventoryItem inventoryItem : inventoryItems) {
            out.append("<p> Deleting InventoryItem #" + inventoryItem.getId().getId() + "</p>");
	          userController.deleteInventoryItem(inventoryItem);
	        }
          out.append("<p> Deleting Inventory #" + inventory.getId().getId() + "</p>");
	        userController.deleteInventory(inventory);
	      }
	      
	      List<StashedItem> stashedItems = itemController.listStashedItemsByUser(user);
	      for (StashedItem stashedItem : stashedItems) {
          out.append("<p> Deleting StashedItem #" + stashedItem.getId().getId() + "</p>");
	        itemController.deleteStashedItem(stashedItem);
	      }

        out.append("<p> Deleting UserIdentifier #" + userIdentifier.getId().getId() + "</p>");
        userController.deleteUserIdentifier(userIdentifier);
	    }
	    
	    UserLocation userLocation = userController.findUserLocationByUser(user);
	    if (userLocation != null) {
        out.append("<p> Deleting UserLocation #" + userLocation.getId().getId() + "</p>");
	      userController.deleteUserLocation(userLocation);
	    }
	    
      out.append("<p> Deleting User #" + user.getId().getId() + "</p>");
      userController.deleteUser(user);
      out.append("<p> <a href=\"" + request.getContextPath() + "/admin/users/listusers.jsp\">Back to list</a></p>"); 
	  } else {
%>
  <h1>Delete user</h1>

  <div>
    Are you sure that you want to delete user #${user.id.id}: ${user.firstName} ${user.lastName} (${user.email})
  </div>

  <form method="POST">
    <input type="hidden" value="${user.id.id}" name="userId""/>
    <input type="submit" value="Delete"/>
  </form>
<%	    
	  }
%>

	<jsp:include page="../fragments/footer.jsp"></jsp:include>
</body>
</html>